<?php

class sxCustomer extends baseCustomerObject {

    private $merchantObj;
    private $usepay;
    private $token;

    public function __construct($wsdl, $sourceKey, $pin, $custNum=null) {
        $this->merchantObj = new sxMerchant($wsdl, $sourceKey, $pin);
        //create instance of the token and from the webservice
        $this->usepay = $this->merchantObj->getClient();
        $this->token = $this->merchantObj->getToken();
        //get customer object values in case of providing custNum
        if ($custNum) {
            $this->CustNum = $custNum;
        }
    }

    //function used to get all customer attribute from the merchant gateway
    public function loadCustomerData() {

        try {
            $cust = $this->usepay->getCustomer($this->token, $this->CustNum);
            $this->Amount = $cust->Amount;
            $this->BillingAddress = $cust->BillingAddress;
            $this->Created = $cust->Created;
            $this->Currency = $cust->Currency;

            $this->CustomData = $cust->CustomData;
            $this->CustomFields = $cust->CustomFields;
            $this->CustomerID = $cust->CustomerID;
            $this->Description = $cust->Description;
            $this->Enabled = $cust->Enabled;
            $this->Failures = $cust->Failures;
            $this->Modified = $cust->Modified;
            $this->Next = $cust->Next;
            $this->Notes = $cust->Notes;
            $this->Next = $cust->Next;
            $this->NumLeft = $cust->NumLeft;
            $this->OrderID = $cust->OrderID;
            $this->PaymentMethods = $cust->PaymentMethods;
            $this->ReceiptNote = $cust->ReceiptNote;
            $this->User = $cust->User;
            $this->URL = $cust->URL;
            $this->Tax = $cust->Tax;
            $this->Source = $cust->Source;
            $this->SendReceipt = $cust->SendReceipt;
            $this->Schedule = $cust->Schedule;
        } catch (Exception $exc) {
            throw new Exception(usaepayErrorHandlee::getError($exc->getMessage()));
        }
    }

    /*
     * The customer will be assigned a unique customer number by the gateway
     *  (CustNum), which you can then use to establish recurring billing cycles,
     *  recall customer data, and manually charge the customer for later products 
     * or services without needing to reenter their information. 
     */
    public function addCustomer($account) {

        $usepay = $this->usepay;
        $token = $this->token;

        try {
            $this->CustNum = $usepay->addCustomer($token, $account);
        } catch (Exception $exc) {
            throw new Exception(usaepayErrorHandlee::getError($exc->getMessage()));
        }
        
        return $this->CustNum;
    }

    /*
     * This method allows you to view all of the stored data for a particular customer.
     * This method requires the use of the CustNum,
     *  a unique customer number assigned by the gateway. 
     * If you have lost or cannot remember the customer's CustNum, 
     * use the searchCustomers method to find the correct CustNum. 
     */

    public function getCustomer() {
        $usepay = $this->usepay;
        $token = $this->token;

        try {
            $Customer = $usepay->getCustomer($token,  $this->CustNum);
        } catch (Exception $exc) {
            throw new Exception(usaepayErrorHandlee::getError($exc->getMessage()));
        }

        return $Customer;
    }

    /*
     * This method allows you to begin a recurring billing cycle 
     * for an individual customer using their CustNum 
     * (unique customer number assigned by the gateway) 
     * as a reference. If you have lost or cannot remember 
     * the customer's CustNum, use the searchCustomerID method to retrieve it. 
     */

    public function enableCustomer() {
        try {
            $results = $this->usepay->enableCustomer($this->token, $this->CustNum);
        } catch (Exception $exc) {
            throw new Exception(usaepayErrorHandlee::getError($exc->getMessage()));
        }
        return $results;
    }

    /*
     * This method allows you to stop a customer's recurring billing cycle 
     * using the CustNum (unique customer number assigned by the gateway) 
     * as a reference. If you have lost or cannot remember 
     * the customer's CustNum, use the searchCustomerID method to retrieve it. 
     */

    public function disableCustomer() {
        try {
            $results = $this->usepay->disableCustomer($this->token, $this->CustNum);
        } catch (Exception $exc) {
            throw new Exception(usaepayErrorHandlee::getError($exc->getMessage()));
        }
        return $results;
    }

    /*
     * Delete the customer.
     */

    public function deleteCustomer() {
        try {
            $results = $this->usepay->deleteCustomer($this->token, $this->CustNum);
        } catch (Exception $exc) {
            throw new Exception(usaepayErrorHandlee::getError($exc->getMessage()));
        }
        return $results;
    }

    /*
     * This method allows you to add a new payment method to a customer record. 
     */

    public function addCustomerPaymentMethod($PaymentMethod, $makeDefult, $verify) {
        try {
            $results = $this->usepay->addCustomerPaymentMethod($this->token, $this->CustNum, $PaymentMethod, $makeDefult, $verify);
        } catch (Exception $exc) {
            throw new Exception(usaepayErrorHandlee::getError($exc->getMessage()));
        }
        return $results;
    }

    /*
     * Retrieve a specific customer payment method
     */

    public function getCustomerPaymentMethod($PaymentMethodID) {
        try {
            $results = $this->usepay->getCustomerPaymentMethod($this->token, $this->CustNum, $PaymentMethodID);
        } catch (Exception $exc) {
            throw new Exception(usaepayErrorHandlee::getError($exc->getMessage()));
        }
        return $results;
    }

    /*
     * Retrieve the customer payment methods
     */

    public function getCustomerPaymentMethods() {
        try {
            $results = $this->usepay->getCustomerPaymentMethods($this->token, $this->CustNum);
        } catch (Exception $exc) {
            throw new Exception(usaepayErrorHandlee::getError($exc->getMessage()));
        }
        return $results;
    }

    /*
     * Delete a payment method from an existing customer. 
     */

    public function deleteCustomerPaymentMethod($PaymentMethodID) {
        try {
            $results = $this->usepay->deleteCustomerPaymentMethod($this->token, $this->CustNum, $PaymentMethodID);
        } catch (Exception $exc) {

            throw new Exception(usaepayErrorHandlee::getError($exc->getMessage()));
        }
        return $results;
    }

    /*
     * Replace all data for customer
     */

    public function updateCustomer($newCustomer) {
        try {
            $results = $this->usepay->updateCustomer($this->token, $this->CustNum, $newCustomer);
        } catch (Exception $exc) {

            throw new Exception(usaepayErrorHandlee::getError($exc->getMessage()));
        }
        return $results;
    }

    /*
     * Update customer data in selected fields only. 
     */

    public function quickUpdateCustomer($fields) {
        try {
            $results = $this->usepay->quickUpdateCustomer($this->token, $this->CustNum, $fields);
        } catch (Exception $exc) {

            throw new Exception(usaepayErrorHandlee::getError($exc->getMessage()));
        }
        return $results;
    }

   
    
    /**
     * @method Run a transaction using payment data stored in the customer database.
     * @param Integer $paymetMethodID 0 for default payement method
     * @param Obj $PgCustomerTransactionRequest
     * @return Obj $results gateway transaction info
     * @author Rana Anwar
     */

    public function runCustomerTransaction($paymetMethodID, $PgCustomerTransactionRequest) {
        try {
            $results = $this->usepay->runCustomerTransaction($this->token, $this->CustNum, $paymetMethodID, $PgCustomerTransactionRequest);
        } catch (Exception $exc) {

            throw new Exception(usaepayErrorHandlee::getError($exc->getMessage()));
        }
        return $results;
    }

    public function updateCustomerPaymentMethod($PaymentMethod, $verify) {
        try {
            $results = $this->usepay->updateCustomerPaymentMethod($this->token, $this->CustNum, $PaymentMethod, $verify);
        } catch (Exception $exc) {

            throw new Exception(usaepayErrorHandlee::getError($exc->getMessage()));
        }
        return $results;
    }
    
}